<template>
  <el-dialog
    :model-value="dialogVisible"
    :title="dialogTitle"
    width="50%"
    @close="handleClose"
  >
    <el-form :model="form" label-width="100px" :rules="rules" ref="formRef">
      <el-form-item label="是否预约">
        <el-switch v-model="form.appointmentid" @click="appointment" />
      </el-form-item>
      <el-form-item label="是否注册车主">
        <el-switch v-model="form.userid" @click="registers" />
      </el-form-item>
      <el-form-item label="车主姓名" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="车主电话" prop="phone">
        <el-input v-model="form.phone" />
      </el-form-item>
      <el-form-item label="汽车品牌" prop="brandId">
        <el-select v-model="form.brandId" clearable placeholder="请选择">
          <el-option
            v-for="item in brandOption"
            :key="item.id"
            :label="item.name"
            :value="item.id"
            :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="汽车颜色" prop="colorId">
        <el-select v-model="form.colorId" clearable placeholder="请选择">
          <el-option
            v-for="item in colorOption"
            :key="item.id"
            :label="item.name"
            :value="item.id"
            :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车辆车型" prop="modelId">
        <el-select v-model="form.modelId" clearable placeholder="请选择">
          <el-option
            v-for="item in modelOption"
            :key="item.id"
            :label="item.name"
            :value="item.id"
            :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="汽车车牌" prop="plateId">
        <el-select v-model="form.plateId" clearable placeholder="请选择">
          <el-option
            v-for="item in plateOption"
            :key="item.id"
            :label="item.name"
            :value="item.id"
            :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属门店" prop="storeId">
        <el-select v-model="form.storeId" clearable placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.companyName"
            :value="item.id"
            :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="到店时间" prop="arriveTime">
        <el-date-picker v-model="form.arriveTime" type="data" style="width: 100%" />
      </el-form-item>
      <el-form-item label="汽车号码" prop="platenum">
        <el-input v-model="form.platenum" />
      </el-form-item>
      <el-form-item label="汽车行驶里程（/km）" prop="mileage">
        <el-input v-model="form.mileage" />
      </el-form-item>
      <el-form-item label="车上物品描述" prop="things">
        <el-input v-model="form.things" />
      </el-form-item>
      <el-form-item label="汽车完好度描述" prop="intactdegrees">
        <el-input v-model="form.intactdegrees" />
      </el-form-item>
      <el-form-item label="用户要求描述" prop="requests">
        <el-input v-model="form.requests" />
      </el-form-item>
      <el-form-item label="创建时间" prop="createtime">
        <el-date-picker v-model="form.createtime" type="data" style="width: 100%" />
      </el-form-item>
      <el-form-item label="保养/维修" prop="type">
        <el-select v-model="form.type" clearable placeholder="请选择">
          <el-option value="2" label="保养">保养</el-option>
          <el-option value="1" label="维修">维修</el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="handleConfirm">添加</el-button>
          <el-button @click="handleClose">取消</el-button>
        </span>
    </template>
    <Appointment v-model="appointmentVisible"
                 :appointmentTitle="appointmentTitle"
                 v-if="appointmentVisible"
                 :appointmentRowValue="appointmentRowValue" />
  </el-dialog>
</template>

<script setup>
import { defineEmits, ref, defineProps, watch, onMounted } from 'vue'
import { save, modify } from '../../../api/checkin'
import { listAll } from '../../../api/company'
import { brandList } from '../../../api/brand'
import { colorList } from '../../../api/color'
import { plateList } from '../../../api/Licenseplate'
import { modelList } from '../../../api/model'
import { saveMaintainRecord } from '../../../api/maintainRecord'
import Appointment from './appointment'
import { ElMessage } from 'element-plus'
import { useStore } from 'vuex'

const form = ref({
  id: '',
  userid: '',
  appointmentid: '',
  name: '',
  phone: '',
  brandId: '',
  colorId: '',
  modelId: '',
  plateId: '',
  storeId: '',
  platenum: '',
  arriveTime: '',
  mileage: '',
  things: '',
  intactdegrees: '',
  requests: '',
  createtime: '',
  type: '',
  checkinId: '',
  des: ''
})

const brandOption = ref([])
const colorOption = ref([])
const modelOption = ref([])
const plateOption = ref([])
const options = ref([])

async function Query() {
  // 车辆品牌
  const brandOptions = await brandList()
  brandOption.value = brandOptions.data.data
  // 车辆颜色
  const colorOptions = await colorList()
  colorOption.value = colorOptions.data.data
  // 车辆车型
  const modelOptions = await modelList()
  modelOption.value = modelOptions.data.data
  // 车辆车牌
  const plateOptions = await plateList()
  plateOption.value = plateOptions.data.data
  // 所属门店
  const query3 = await listAll()
  options.value = query3.data.data
}

// vue组件加载完成后执行
onMounted(() => {
  Query()
})

const appointmentVisible = ref(false)
const appointmentTitle = ref('')
const appointmentRowValue = ref({})

// 是否预约
function appointment() {
  appointmentTitle.value = '选择预约记录'
  appointmentRowValue.value = {}
  appointmentVisible.value = true
}

const store = useStore()

// 是否预约
function registers() {
  if (store.getters.getAppointment != null) {
    const appointment = store.getters.getAppointment
    form.value = JSON.parse(JSON.stringify(appointment))
    console.log(JSON.parse(JSON.stringify(appointment)))
  }
}

// onMounted(() => {
//   registers()
// })

const props = defineProps({
  // 标题
  dialogTitle: {
    type: String,
    default: '',
    required: true
  },
  // 编辑行数据
  dialogRowValue: {
    type: Object,
    default: () => {}
  }
})
// 监听props.dialogRowValue值改变
watch(
  () => props.dialogRowValue,
  () => {
    form.value = props.dialogRowValue
    // 判断是否为空 不是则将本地的数据共享的值存入form.value
    if (store.getters.getAppointment != null) {
      const appointment = store.getters.getAppointment
      form.value = JSON.parse(JSON.stringify(appointment))
      console.log(JSON.parse(JSON.stringify(appointment)))
    }
  },
  { deep: true, immediate: true }
)

const isDisabled = ref(false)
onMounted(() => {
  if (props.dialogTitle === '编辑') {
    isDisabled.value = true
  }
})

// 表单校验
const rules = ref({
  name: [
    { required: true, message: '职位不能为空' }
  ]
})

// 修改其他属性的值
const emits = defineEmits(['update:modelValue', 'query'])
// 关闭
function handleClose() {
  emits('update:modelValue', false)
}
// 确定
const formRef = ref(null)
function handleConfirm() {
  formRef.value.validate(async (valid) => {
    if (valid) {
      props.dialogTitle === '添加记录' ? await save(form.value) : await modify(form.value)
      const che = form.value.id
      form.value.checkinId = che
      console.log(che)
      saveMaintainRecord(form.value)
      ElMessage({
        message: '添加记录成功且添加为保养明细',
        type: 'success'
      })
      // 上层组件重新获取数据
      emits('query')
      handleClose()
    } else {
      return false
    }
  })
}
</script>

<style class="scss" scoped>
::v-deep .el-input__prefix-inner {
  align-items: center;
}
</style>
